using System.Collections.Generic;
using UnityEngine;
using FairyGUI;

public class WeaponMO {
    static List<Weapon> _weaponDataList = null;//所有武器数

    private static void InitData()
    {
        if (_weaponDataList != null) return;
        //读取装备数据
        _weaponDataList = Utils.LoadByJson<List<Weapon>>(Application.dataPath, "weaponList.json");
        if (_weaponDataList == null) _weaponDataList = new List<Weapon>();

        //重新创建单个大项属性，保存为json后转回来，就变成父类的对象
        for(int i = 0;i < _weaponDataList.Count;i++){
            for(int j = 0;j < _weaponDataList[i].skill.Count;j++){
                // var name1 = _weaponDataList[i].skill[j].className;
                // var name2 = _weaponDataList[i].skill[j].GetType().FullName;
                // if(name1 != name2){
                    // _weaponDataList[i].skill[j].className = "WeaponSkill";
                    var propertyList = _weaponDataList[i].skill[j].propertyList;
                    var assembly = Utils.GetCurAssembly();
                    _weaponDataList[i].skill[j] = (SkillBase)assembly.CreateInstance(_weaponDataList[i].skill[j].className); 
                    _weaponDataList[i].skill[j].InitData(propertyList);
                // }
            }
        }
    }
    /// <summary>
    /// 获取所有武器数据列表
    /// </summary>
    /// <returns></returns>
    public static List<Weapon> GetWeaponList(){
        InitData();
        return _weaponDataList;
    }
    /// <summary>
    /// 保存武器列表
    /// </summary>
    public static void SaveWeaponDataList(){
        if(_weaponDataList == null) return;
        Utils.SaveByJson<List<Weapon>>(Application.dataPath, "weaponList.json",_weaponDataList);
    }
    /// <summary>
    /// 保存武器
    /// </summary>
    /// <param name="weaponData"></param>
    /// <param name="index"></param>
    /// <returns></returns>
    public static bool SaveWeapon(Weapon weaponData,int index)
    {
        //新增武器的保存
        if(index == _weaponDataList.Count) {
            _weaponDataList.Add(weaponData);
        }
        else{
            if (index < 0 || index >= _weaponDataList.Count) return false;
            _weaponDataList[index] = weaponData;
        }
        SaveWeaponDataList();
        return true;
    }
    /// <summary>
    /// 删除武器
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public static bool DelWeapon(int index){
        if (index < 0 || index >= _weaponDataList.Count) return false;
        LTLocalization.DelKey(_weaponDataList[index].name);
        //从套装内移除
        SuitMO.RemoveAllPointWeapon(_weaponDataList[index].id);
        //从生物中移除
        BiologyMO.RemoveAllPointCard(_weaponDataList[index].id);
        _weaponDataList.RemoveAt(index);
        //ResetWeaponId();
        SaveWeaponDataList();
        LTLocalization.SaveDataToCSV();
        return true;
    }
    /// <summary>
    /// 重置武器id
    /// </summary>
    public static void ResetWeaponId(){
        if(_weaponDataList == null) return;
        for(int i = 0;i < _weaponDataList.Count;i++){
            _weaponDataList[i].id = "Weapon_"+(i + 1);
            ResetWeaponName(_weaponDataList[i]);
        }
    }
    /// <summary>
    /// 重置武器名字键值
    /// </summary>
    /// <param name="weapon"></param>
    public static void ResetWeaponName(Weapon weapon)
    {
        var newName = weapon.id + "_name";
        if(newName != weapon.name)
        {
            var isContain = LTLocalization.IsContain(weapon.name);
            if (isContain)
            {
                LTLocalization.ReplaceKey(newName,weapon.name);
                LTLocalization.DelKey(weapon.name);
            }
            weapon.name = newName;
        }
    }
    /// <summary>
    /// 获取指定武器下标
    /// </summary>
    /// <param name="weapon"></param>
    /// <returns></returns>
    public static int GetWeaponIndex(Weapon weapon){
        InitData();
        return _weaponDataList.IndexOf(weapon);
    }
    /// <summary>
    /// 获取指定id的数据
    /// </summary>
    /// <param name="weaponId"></param>
    /// <returns></returns>
    public static Weapon GetWeaponById(string weaponId){
        InitData();
        foreach(var item in _weaponDataList){
            if(item.id == weaponId) return item;
        }
        return null;
    }
}
